package com.lhjmall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhjmall.common.utils.PageUtils;
import com.lhjmall.common.utils.Query;
import com.lhjmall.common.utils.R;
import com.lhjmall.product.dao.ProductDao;
import com.lhjmall.product.dto.ProductDto;
import com.lhjmall.product.entity.ProductEntity;
import com.lhjmall.product.fegin.IWareFegin;
import com.lhjmall.product.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {


	@Autowired
	private IWareFegin wareFegin;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public ProductDto getByIdForC(Integer id) throws Exception {
		ProductEntity productEntity=this.getById(id);
		if(productEntity==null){
			throw new Exception("商品不存在");
		}
		ProductDto productDto=new ProductDto();
		org.apache.commons.beanutils.BeanUtils.copyProperties(productDto, productEntity);
		R result=wareFegin.getWareByProductId(id);
		Object data=result.get("productware");
		if(data!=null){
			JSONObject jsonObject=JSONObject.parseObject(data.toString().replaceAll("=", ":"));
			productDto.setNum(Integer.parseInt(jsonObject.get("num").toString()));
		}
		return productDto;
	}

}